package com.scrcu.cloud.wechat.home.endpoint.customer.controller;

import com.scrcu.cloud.wechat.home.endpoint.common.model.RestResponse;
import com.scrcu.cloud.wechat.home.endpoint.customer.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService iUserService;

    @PostMapping("/getOpenid")
    public RestResponse getOpenid(@RequestParam("code") String code,
                                  @RequestParam(name = "appid",required = false) String appid){
        return iUserService.getOpenid(code,appid);
    }

    /**
     * 腾讯小程序接口调用 --
     * 获取小程序access_token
     * */
    @PostMapping("/getToken")
    public RestResponse getToken(@RequestParam(name = "appid") String appid){
        return iUserService.getToken(appid);
    }

    /**
     * 腾讯多端应用接口调用规范--使用多端应用secret和应用id
     * 根据用户的code获取用户唯一id和手机号、登录位置、登录设备等重要信息
     * @return 根据情况返回map格式的内容
     * */
    @PostMapping("/getUserInfo")
    public RestResponse getUserInfo(@RequestParam("code") String code,
                                    @RequestParam(name = "appid",required = false) String appid){
        return iUserService.getUserInfo(code,appid);
    }

    /**
     * 腾讯小程序接口调用 --
     * 根据用户的code和小程序access_token获取用户手机号信息
     * */
    @PostMapping("/getPhoneInfo")
    public RestResponse getPhoneInfo(@RequestParam("code") String code,
                                  @RequestParam(name = "token") String token){
        return iUserService.getPhoneInfo(code,token);
    }

    /**
     * 后端代码调用接口
     * 根据该小程序的openid判断该用户是否已在小程序中注册
     * */
    @PostMapping("/judgeUser")
    public RestResponse judgeUser(@RequestParam("openid") String openid,
                                  @RequestParam("last_login_location") String last_login_location){
        return iUserService.judgeUser(openid,last_login_location);
    }

    /**
     * 后端代码调用接口
     * 注册用户信息到数据库
     * */
    @PostMapping("/registerUser")
    public RestResponse registerUser(@RequestParam("uname") String uname,
                                     @RequestParam("telnum") String telnum,
                                     @RequestParam("idcard") String idcard,
                                     @RequestParam("openid") String openid,
                                     @RequestParam("register_location") String register_location){
        return iUserService.registerUser(uname,telnum,idcard,openid,register_location);
    }

    @PostMapping("/getMyInfo")
    public RestResponse getMyInfo(@RequestParam("openid") String openid){
        return iUserService.getMyInfo(openid);
    }

    @PostMapping("/changeMyInfo")
    public RestResponse changeMyInfo(@RequestParam("openid") String openid,
                                         @RequestParam("todoType") Integer todoType,
                                         @RequestParam("value") String value){
        return iUserService.changeMyInfo(openid,todoType,value);
    }

}
